﻿using System;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.Keyence
{
	/// <summary>
	/// 基恩士的KV链路模式，PLC的两个端口默认9600bps,8位数据位，1位起始位，1位停止位，偶校验位，无校验和
	/// </summary>
	/// <remarks>
	/// 使用Kegence自己的协议进行通讯时使用的模式。对外围设备发送过来的指令，将自动返回响应。 
	/// CPU单元侧无需通讯程序。可与 PC 等连接。支持KV-5500/5000/3000
	/// </remarks>
	// Token: 0x0200008B RID: 139
	public class KeyenceKVSerial : SerialDeviceBase
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06000ACC RID: 2764 RVA: 0x00040532 File Offset: 0x0003E732
		public KeyenceKVSerial()
		{
			base.ByteTransform = new RegularByteTransform();
			base.WordLength = 1;
		}

		/// <inheritdoc />
		// Token: 0x06000ACD RID: 2765 RVA: 0x00040550 File Offset: 0x0003E750
		protected override OperateResult InitializationOnOpen()
		{
			OperateResult<byte[]> operateResult = base.ReadBase(KeyenceNanoSerialOverTcp.ConnectCmd);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				bool flag2 = operateResult.Content.Length > 2;
				if (flag2)
				{
					bool flag3 = operateResult.Content[0] == 67 && operateResult.Content[1] == 67;
					if (flag3)
					{
						return OperateResult.CreateSuccessResult();
					}
				}
				result = new OperateResult("Check Failed: " + SoftBasic.ByteToHexString(operateResult.Content, ' '));
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000ACE RID: 2766 RVA: 0x000405D4 File Offset: 0x0003E7D4
		protected override OperateResult ExtraOnClose()
		{
			OperateResult<byte[]> operateResult = base.ReadBase(KeyenceNanoSerialOverTcp.DisConnectCmd);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				bool flag2 = operateResult.Content.Length > 2;
				if (flag2)
				{
					bool flag3 = operateResult.Content[0] == 67 && operateResult.Content[1] == 70;
					if (flag3)
					{
						return OperateResult.CreateSuccessResult();
					}
				}
				result = new OperateResult("Check Failed: " + SoftBasic.ByteToHexString(operateResult.Content, ' '));
			}
			return result;
		}
	}
}
